#ifndef MODBUS_CONFIG_H_
#define MODBUS_CONFIG_H_

#define LIBMODBUS_VERSION_STRING "24.00.00.01"

#if (TARGET_OS == OS_WIN)

#elif (TARGET_OS == OS_POSIX)

#include "config-posix.h"

#elif (TARGET_OS == OS_MACH)

#include "config-mach.h"

#endif

#if defined(HAVE_BYTESWAP_H)
#include <byteswap.h>
#endif

#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define bswap_16 OSSwapInt16
#define bswap_32 OSSwapInt32
#define bswap_64 OSSwapInt64
#endif

#if defined(__GNUC__)
#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__ * 10)
#if GCC_VERSION >= 430
#undef bswap_32
#define bswap_32 __builtin_bswap32
#endif
#if GCC_VERSION >= 480
#undef bswap_16
#define bswap_16 __builtin_bswap16
#endif
#endif

#if defined(_MSC_VER) && (_MSC_VER >= 1400)
#define bswap_32 _byteswap_ulong
#define bswap_16 _byteswap_ushort
#endif

#if !defined(bswap_16)
#warning "Fallback on C functions for bswap_16"
static inline uint16_t bswap_16(uint16_t x)
{
    return (x >> 8) | (x << 8);
}
#endif

#if !defined(bswap_32)
#warning "Fallback on C functions for bswap_32"
static inline uint32_t bswap_32(uint32_t x)
{
    return (bswap_16(x & 0xffff) << 16) | (bswap_16(x >> 16));
}
#endif

#if defined(_WIN32)
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif

#if HAVE_DECL_TIOCSRS485 || HAVE_DECL_TIOCM_RTS
#include <sys/ioctl.h>
#endif

#if HAVE_DECL_TIOCSRS485
#include <linux/serial.h>
#endif

#if !defined(MSG_NOSIGNAL)
#define MSG_NOSIGNAL 0
#endif

#if defined(_AIX) && !defined(MSG_DONTWAIT)
#define MSG_DONTWAIT MSG_NONBLOCK
#endif

// clang-format off
#if defined(_WIN32)
# define OS_WIN32
# ifndef WINVER
#   define WINVER 0x0501
# endif
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <signal.h>
#include <sys/types.h>

#if defined(_WIN32)

# include <winsock2.h>
# include <ws2tcpip.h>
# define SHUT_RDWR 2
# define close closesocket
# define strdup _strdup

#else
# include <sys/socket.h>
# include <sys/ioctl.h>

#if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD__ < 5)
# define OS_BSD
# include <netinet/in_systm.h>
#endif

# include <netinet/in.h>
# include <netinet/ip.h>
# include <netinet/tcp.h>
# include <arpa/inet.h>
# include <netdb.h>
#endif


// clang-format off
#ifndef _MSC_VER
#include <sys/time.h>
#else
#include <time.h>
//typedef int ssize_t;
#endif


#if defined(_WIN32)
#include <windows.h>
#else
#include <termios.h>
#endif

#endif